﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WoChu.RfClient.Entity;
using WoChu.RfClient.Common;
using WoChu.CF.BaseFrame.Util;

namespace WoChu.RfClient.Duty
{
    public partial class FrmProfitAndLoss : Form
    {
        public FrmProfitAndLoss()
        {
            InitializeComponent();
            InitProfitAndLossTypeInfo();
            initDataGridSyInfo();
        }

        #region 初始化数据
        /// <summary>
        /// 初始化损益类型
        /// </summary>
        private void InitProfitAndLossTypeInfo()
        {
            List<LookUpItemInfo> lstProfitAndLossType = Duty.ProfitAndLossUIRule.NewInstance.Query("PROFITANDLOSSTYPE");
            if (lstProfitAndLossType.Count > 0)
            {
                this.cboxSyType.DataSource = lstProfitAndLossType;
                this.cboxSyType.DisplayMember = "LOOKUP";
                this.cboxSyType.ValueMember = "FIELDVALUE";

            }
        }

        /// <summary>
        /// 初始化数据源表头
        /// </summary>
        private void initDataGridSyInfo()
        {
            DataSet ds = new DataSet();//创建DataSet  

            DataTable dt = new DataTable("ProfitAndLossInfo");//创建一个名为ProfitAndLossInfo的DataTalbe  

            dt.Columns.Add(new DataColumn("序号", typeof(int)));//为dt_dry表内建立Column  
            dt.Columns.Add(new DataColumn("损益类型", typeof(string)));
            dt.Columns.Add(new DataColumn("商品编号", typeof(string)));
            dt.Columns.Add(new DataColumn("商品名称", typeof(string)));
            dt.Columns.Add(new DataColumn("数量", typeof(int)));
            ds.Tables.Add(dt);
            this.dataGridSyInfo.DataSource = ds.Tables[0];
        }

        #endregion

        #region Form事件
        /// <summary>
        /// 扫描枪扫描事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtShopNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            try
            {
                int rowNumber = 1;
                bool b = false;
                string _ShopNo = this.txtShopNo.Text.Trim();
                if (string.IsNullOrEmpty(_ShopNo))
                {
                    MessageBox.Show("订单号不能为空！");
                    return;
                }

                //根据商品编号获取商品名称
                string goodsName = Duty.ProfitAndLossUIRule.NewInstance.GetGoodsNameByCode(_ShopNo);

                if (string.IsNullOrEmpty(goodsName))
                {
                    goodsName = "无";
                }

                DataSet dsDG = new DataSet();
                DataTable dt = (dataGridSyInfo.DataSource as DataTable).Copy();
                if (dt != null && dt.Rows.Count > 0)//如果是相同的商品不增加新数据只在原来基础上数量+1
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (_ShopNo == dt.Rows[i]["商品编号"].ToString())
                        {

                            dt.Rows[i]["数量"] = Convert.ToInt32(dt.Rows[i]["数量"].ToString()) + 1;
                            b = true;
                        }
                    }
                    rowNumber = Convert.ToInt32(dt.Rows[dt.Rows.Count - 1]["序号"].ToString()) + 1;
                }

                if (!b)
                {
                    DataRow dr = dt.NewRow();
                    dr = dt.NewRow();
                    dr["序号"] = rowNumber.ToString();
                    dr["损益类型"] = this.cboxSyType.Text;
                    dr["商品编号"] = _ShopNo;
                    dr["商品名称"] = goodsName;
                    dr["数量"] = 1;
                    dt.Rows.Add(dr);
                    dsDG.Tables.Add(dt);
                    this.dataGridSyInfo.DataSource = dsDG.Tables[0];
                    txtShopNo.Text = string.Empty;
                    txtShopNo.Focus();

                }
                else
                {
                    dsDG.Tables.Add(dt);
                    this.dataGridSyInfo.DataSource = dsDG.Tables[0];
                    txtShopNo.Text = string.Empty;
                    txtShopNo.Focus();
                }

                this.cboxSyType.Enabled = false;

            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }



        }

        /// <summary>
        /// 提交损益数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConmit_Click(object sender, EventArgs e)
        {
            DataTable dt = (dataGridSyInfo.DataSource as DataTable).Copy();
            List<ProfitAndLossDetailInfo> listPALD = new List<ProfitAndLossDetailInfo>();

            ProfitAndLossDetailInfo padl = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                ProfitAndLossInfo _order = new ProfitAndLossInfo();
                _order.ADDDATETIME = DateTime.Now.ToString();
                _order.PALID = 1;
                _order.OPRNO = LoginBean.Instance.USERCODE;
                _order.STATUS = "1";

                //生产订单号
                _order.PALORDERNO = DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    padl = new ProfitAndLossDetailInfo();
                    padl.PALORDERNO = _order.PALORDERNO;
                    padl.GOODSNO = dt.Rows[i]["商品编号"].ToString();
                    padl.GOODSNUMBER = Convert.ToInt32(dt.Rows[i]["数量"].ToString());
                    padl.PALTYPE = this.cboxSyType.SelectedValue.ToString();
                    listPALD.Add(padl);
                }
                //提交损益信息
                bool b = Duty.ProfitAndLossUIRule.NewInstance.InsertProfitAndLoss(_order.PALORDERNO, LoginBean.Instance.USERCODE, listPALD);
                if (b)//如果成功清空当前损益信息
                {
                    MessageBox.Show("数据提交成功！");
                    initDataGridSyInfo();
                    txtShopNo.Text = string.Empty;
                    txtShopNo.Focus();
                    this.cboxSyType.Enabled = true;
                }
                else
                {
                    MessageBox.Show("数据提交失败！");
                }

            }
            else
            {
                MessageBox.Show("请先扫描损益信息后再提交数据！");
                return;
            }



        }

        //删除选择损益信息
        private void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteRow = this.dataGridSyInfo.CurrentRowIndex;
            if (deleteRow >= 0)
            {
                DialogResult dr = MessageBox.Show("您确定要删除这条数据吗?", "删除数据", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                if (dr == DialogResult.Yes)//如果点击“确定”按钮
                {
                    DataSet dsDG = new DataSet();
                    DataTable dt = (dataGridSyInfo.DataSource as DataTable).Copy();
                    dt.Rows.RemoveAt(this.dataGridSyInfo.CurrentRowIndex);
                    dsDG.Tables.Add(dt);
                    this.dataGridSyInfo.DataSource = dsDG.Tables[0];
                    txtShopNo.Focus();
                }

            }
            else
            {
                MessageBox.Show("请先选择一条数据再删除!");
            }

        }

        /// <summary>
        /// 修改商品数量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {

            DataSet dsDG = new DataSet();
            DataTable dt = (dataGridSyInfo.DataSource as DataTable).Copy();
            if (dt != null && dt.Rows.Count > 0)
            {
                try
                {
                    dt.Rows[this.dataGridSyInfo.CurrentRowIndex]["数量"] = Convert.ToInt32(this.txtGoodsNumber.Text.Trim());
                    dsDG.Tables.Add(dt);
                    this.dataGridSyInfo.DataSource = dsDG.Tables[0];
                    txtShopNo.Focus();
                }
                catch (Exception)
                {
                    MessageBox.Show("请正确输入商品数量!");
                }

            }
        }

        /// <summary>
        /// 获得点击行商品数量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridSyInfo_DoubleClick(object sender, EventArgs e)
        {
            DataTable dt = (dataGridSyInfo.DataSource as DataTable).Copy();
            if (dt != null && dt.Rows.Count > 0)
            {
                this.txtGoodsNumber.Text = dt.Rows[this.dataGridSyInfo.CurrentRowIndex]["数量"].ToString();
            }
        }


        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmProfitAndLoss_Closing(object sender, CancelEventArgs e)
        {
            DataTable dt = (dataGridSyInfo.DataSource as DataTable).Copy();
            if (dt != null && dt.Rows.Count > 0)
            {
                DialogResult dr = MessageBox.Show("您还有数据尚未提交是否确认退出?", "退出", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                if (dr == DialogResult.Yes)//如果点击“确定”按钮
                {
                    //System.Reflection.Assembly ass = System.Reflection.Assembly.Load("WoChu.RfClient.Login");
                    //Type type = ass.GetType("WoChu.RfClient.Login.FrmMain");
                    //Object o = Activator.CreateInstance(type);
                    //System.Reflection.MethodInfo mi = type.GetMethod("Show");

                    //mi.Invoke(o, null);

                    this.Close();
                    this.Dispose();
                }
                else
                {
                    e.Cancel = true;
                }

            }
            else
            {
                //System.Reflection.Assembly ass = System.Reflection.Assembly.Load("WoChu.RfClient.Login");
                //Type type = ass.GetType("WoChu.RfClient.Login.FrmMain");
                //Object o = Activator.CreateInstance(type);
                //System.Reflection.MethodInfo mi = type.GetMethod("Show");

                //mi.Invoke(o, null);

                this.Close();
                this.Dispose();
            }
        }

        #endregion



    }
}